package com.scau.ums.service.imp.RepairReport;

import com.scau.ums.mapper.CustomedFillRepairResult_Material;
import com.scau.ums.mapper.RepairtaskorderMapper;
import com.scau.ums.model.Repairmaterial;
import com.scau.ums.model.Repairreport;
import com.scau.ums.model.Repairresult;
import com.scau.ums.model.Repairtaskorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FillRepairResult_MaterialImp {
    final static int pageSize=8;
    @Autowired
    private CustomedFillRepairResult_Material customedFillRepairResult_Material;
    @Autowired
    private RepairtaskorderMapper repairTaskOrderMapper;

    public void FillRepairResult(Repairresult repairResult){
        customedFillRepairResult_Material.FillRepairResult(repairResult);
        Repairtaskorder repairTaskOrder=repairTaskOrderMapper.selectByPrimaryKey(repairResult.getRepairorderid());
        customedFillRepairResult_Material.UpdateRepairResultState(repairTaskOrder.getReportid());
    }


    public void FillRepairMaterial(Repairmaterial repairMaterial){
        customedFillRepairResult_Material.FillRepairMaterial(repairMaterial);
    }

    public Repairresult CheckRepairResultExistence(Integer repairorderid){
        return customedFillRepairResult_Material.CheckRepairResultExistence(repairorderid);
    }

    public void rewriteRepairResult(Repairresult repairResult){
        customedFillRepairResult_Material.rewriteRepairResult(repairResult);
    }


    public int getRepairMaterialPageSum(String searchKeyword) {
        int totalRecord = customedFillRepairResult_Material.getMaterialNumber(searchKeyword);//总数据记录
        double tmp = (double)totalRecord/pageSize;
        int pageSum = (int) Math.ceil(tmp);//总页数
        return pageSum;
    }

    public List<Repairtaskorder> getTaskMaterialListByLimit(int curPage, String searchKeyword){
        int start = (curPage - 1) * pageSize;//当前开始记录下标
        return customedFillRepairResult_Material.getTaskMaterialListByLimit(start,pageSize,searchKeyword);
    }

    public int getRepairResultPageSum() {
        int totalRecord = customedFillRepairResult_Material.getResultNumber();//总数据记录
        double tmp = (double)totalRecord/pageSize;
        int pageSum = (int) Math.ceil(tmp);//总页数
        return pageSum;
    }

    public List<Repairtaskorder> getRepairResultListByLimit(int curPage){
        int start = (curPage - 1) * pageSize;//当前开始记录下标
        List<Integer> repairTaskOrderIds=customedFillRepairResult_Material.getRepairTaskOrderIds(start,pageSize);
        List<Repairtaskorder> repairResultList=new ArrayList<>();
        for(int i=0;i<repairTaskOrderIds.size();i++){
            int id=repairTaskOrderIds.get(i);
            Repairtaskorder repairTaskOrder = repairTaskOrderMapper.selectByPrimaryKey(id);
            repairResultList.add(repairTaskOrder);
        }
        return repairResultList;

    }
}
